package ferp.android.dialogs;

import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.text.Html;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import ferp.android.GUI;
import ferp.android.R;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

/**
 * User: olegs
 * Date: 19/05/2014 09:41
 */
public abstract class ChoiceDialog extends Dialog
{
  static class ListData
  {
    interface OnItemSafeClickListener
    {
      void onItemClicked(int i);
    }

    public ListData(Context context, TextView tv, ListView lv, int mode, int adapterLayoutResource)
    {
      this.context = context;
      text = tv;
      list = lv;

      text.setTypeface(GUI.Font.BOLD);
      list.setChoiceMode(mode);

      options = new LinkedList<CharSequence>();

      adapter = new ArrayAdapter<CharSequence>(context, adapterLayoutResource, android.R.id.text1, options)
      {
        @Override
        public View getView(int position, View convertView, ViewGroup parent)
        {
          TextView tv = (TextView)super.getView(position, convertView, parent);

          tv.setTypeface(GUI.Font.BOLD);
          tv.setTextColor(isEnabled(position) ? Color.WHITE : GUI.Color.DISABLE);
          tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, TEXT_SIZE);

          return tv;
        }

        @Override
        public boolean isEnabled(int position)
        {
          // current solution for backward compatibility with dialogs other than CloudActionsDialog
          if (enabled.size() == 0)
            return true;

          return enabled.get(position);
        }
      };

      list.setAdapter(adapter);

      list.setOnItemClickListener(new AdapterView.OnItemClickListener()
      {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l)
        {
          if (oiscl != null && !clicked)
          {
            clicked = true;

            oiscl.onItemClicked(i);
          }
        }
      });
    }

    protected void onShow() {clicked = false;}

    protected void setOnItemSafeClickListener(OnItemSafeClickListener oiscl) {this.oiscl = oiscl;}

    protected void set(String[] options) {set(Arrays.asList(options));}
    protected void set(List<String> options) {set(options, ListView.INVALID_POSITION);}
    protected void set(CharSequence text, List<String> options) {set(text, options, ListView.INVALID_POSITION);}
    protected void set(List<String> options, int selected) {set(null, options, selected);}

    protected void set(int resText, List<String> options, int selected)
    {
      set(resText == View.NO_ID ? null : context.getString(resText), options, selected);
    }

    protected void set(CharSequence text, List<String> options, int selected)
    {
      // reset clicked flag to allow item selection
      clicked = false;

      // set text view
      if (text == null)
        this.text.setVisibility(View.GONE);
      else
      {
        this.text.setText(text);
        this.text.setVisibility(View.VISIBLE);
      }

      // reset options
      this.options.clear();
      for (String s : options)
        this.options.add(Html.fromHtml(s));

      // invalidate view
      invalidate(selected);
    }

    protected void invalidate() {invalidate(ListView.INVALID_POSITION);}

    protected void invalidate(int selected)
    {
      // invalidate adapter
      adapter.notifyDataSetChanged();
      // clear choices
      list.clearChoices();
      // set selected item
      list.setItemChecked(selected, true);
    }

    protected Context context;
    protected final TextView text;
    protected final ListView list;

    protected final List<CharSequence> options;
    protected final ArrayAdapter<CharSequence> adapter;
    protected final ArrayList<Boolean> enabled = new ArrayList<Boolean>();

    protected OnItemSafeClickListener oiscl;

    // ensure that item could be clicked only once
    protected boolean clicked;

    private final static int TEXT_SIZE = 16;
  }

  public ChoiceDialog(Context context, int resLayout)
  {
    super(context, resLayout);

    findViewById(R.id.dlg_element_footer_delimiter).setVisibility(getFooterVisibility());
    findViewById(R.id.dlg_element_footer_layout).setVisibility(getFooterVisibility());

    ld = new ListData(context, (TextView)(findViewById(R.id.dlg_element_list_view_body).findViewById(R.id.lvb_text)),
        (ListView)(findViewById(R.id.dlg_element_list_view_body).findViewById(R.id.lvb_list)),
        getMode(),
        getAdapterLayoutResource());

    setOnShowListener(new OnShowListener()
    {
      @Override
      public void onShow(DialogInterface dialogInterface)
      {
        ld.onShow();
      }
    });
  }

  protected abstract int getMode();
  protected abstract int getAdapterLayoutResource();
  protected abstract int getFooterVisibility();

  protected final ListData ld;
}
